﻿using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionCore;
using ZedGraph;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TrackBar;

namespace COER.Manual
{
    public partial class UC_CCD_Ctr : UserControl
    {

        private int CCD_Index = 0;
        private string Name = "CCD1";
        private MotionHelper motion;
        Point[] LocatePos = new Point[20];
        private int TrackSW = 0;


        private double Speed = 50;
        private double xyOffset { get => (double.TryParse(tbx_Offset.Text, out double temp) ? temp  : 0); }
        private double zrOffset { get => (double.TryParse(tbx_Offset.Text, out double temp) ? temp : 0); }


        public UC_CCD_Ctr()
        {
            InitializeComponent();
            GetControls(this.Controls);
        }

        public void Load()
        {
            this.gbx_Control.Text = Name;
            timer1.Start();
        }
        private void GetControls(Control.ControlCollection ctrls)
        {
            foreach (Control item in ctrls)
            {
                if (item is Button && item.Tag?.ToString().Split('|')[0] == "btn")
                {
                    ((Button)item).Click += new EventHandler(this.btn_Switch_Click);
                }

                //if (item is Button && item.Tag?.ToString().Split('|')[0] == "btn")
                //{
                //    ((Button)item).OP = Kernel.OP;
                //}

                if (item.Controls != null)
                {
                    GetControls(item.Controls);
                }
            }
        }

        public void Init(int index, string name,MotionHelper m)
        {
            this.CCD_Index = index;
            this.Name = name;
            this.motion = m;
            cmb_InspChange.SelectedIndex = 1;
        }
        private void btn_Switch_Click(object sender, EventArgs e)
        {
                if (cmb_InspChange.SelectedItem==null) return;
                           
                if (cmb_InspChange.SelectedItem == "检测轨道1")
                {
                    TrackSW = 1;
                }
                else if (cmb_InspChange.SelectedItem == "检测轨道2")
                {
                    TrackSW = 2;
                }               
            
                Button btn = (Button)sender;
                string[] info = btn.Tag.ToString().Split('|');

                switch (info[1])
                {
                case "CCD_X_POS":
                    {
                        CCD_Ctr(CCD_Index, 1, true);
                    }                         
                        break;
                    case "CCD_X_NEG":
                    {
                        CCD_Ctr(CCD_Index, 1, false);
                    }
                    break;      
                    case "CCD_Y_POS":
                    {
                        CCD_Ctr(CCD_Index, 2, true);
                    }
                    break;
                case "CCD_Y_NEG":
                    {
                        CCD_Ctr(CCD_Index, 2, false);
                    }
                    break;
                case "CCD_Z_POS":
                    {
                        CCD_Ctr(CCD_Index, 3, true);
                    }
                    break;
                case "CCD_Z_NEG":
                    {
                        CCD_Ctr(CCD_Index, 3, false);
                    }
                    break;
                case "CCD_Insp1Mark":
                    {
                        CCD_Ctr(CCD_Index,1, TrackSW);
                    }
                    break;
                case "CCD_Insp1Wait":
                    {
                        CCD_Ctr(CCD_Index, 5, TrackSW);
                    }
                    break;
                case "CCD_Insp1Work":
                    {
                        CCD_Ctr(CCD_Index, 2, TrackSW);
                    }
                    break;


            }
        }

        /// <summary>
        /// 相机定位控制
        /// </summary>
        /// <param name="ccdIdx">CCD编号</param>
        /// <param name="axisID">定位编号</param>      
        private void CCD_Ctr(int ccdIdx,int axisID,bool dir)
        {
            if (CCD_Index == 1)
            {
                if (dir)
                {
                    if (axisID == 1)
                    {
                        motion.CCD1_X.AxisMoveAbs(motion.CCD1_X.CurrentCmdPos + xyOffset, 1, Speed, 1, 1);
                    }
                    else if (axisID == 2)
                    {
                        motion.CCD1_Y.AxisMoveAbs(motion.CCD1_Y.CurrentCmdPos + xyOffset, 1, Speed, 1, 1);
                    }
                    else if (axisID == 3) 
                    {
                        motion.CCD1_Z.AxisMoveAbs(motion.CCD1_Z.CurrentCmdPos + xyOffset, 1, Speed, 1, 1);
                    }                   
                }
                else
                {
                    if (axisID == 1)
                    {
                        motion.CCD1_X.AxisMoveAbs(motion.CCD1_X.CurrentCmdPos - xyOffset, 1, Speed, 1, 1);
                    }
                    else if (axisID == 2)
                    {
                        motion.CCD1_Y.AxisMoveAbs(motion.CCD1_Y.CurrentCmdPos - xyOffset, 1, Speed, 1, 1);
                    }
                    else if (axisID == 3)
                    {
                        motion.CCD1_Z.AxisMoveAbs(motion.CCD1_Z.CurrentCmdPos - xyOffset, 1, Speed, 1, 1);
                    }
                }
            }
            else if (CCD_Index == 2)
            {
                if (dir)
                {
                    if (axisID == 1)
                    {
                        motion.CCD2_X.AxisMoveAbs(motion.CCD2_X.CurrentCmdPos + xyOffset, 1, Speed, 1, 1);
                    }
                    else if (axisID == 2)
                    {
                        motion.CCD2_Y.AxisMoveAbs(motion.CCD2_Y.CurrentCmdPos + xyOffset, 1, Speed, 1, 1);
                    }
                    else if (axisID == 3)
                    {
                        motion.CCD2_Z.AxisMoveAbs(motion.CCD2_Z.CurrentCmdPos + xyOffset, 1, Speed, 1, 1);
                    }
                }
                else
                {
                    if (axisID == 1)
                    {
                        motion.CCD2_X.AxisMoveAbs(motion.CCD2_X.CurrentCmdPos - xyOffset, 1, Speed, 1, 1);
                    }
                    else if (axisID == 2)
                    {
                        motion.CCD2_Y.AxisMoveAbs(motion.CCD2_Y.CurrentCmdPos - xyOffset, 1, Speed, 1, 1);
                    }
                    else if (axisID == 3)
                    {
                        motion.CCD2_Z.AxisMoveAbs(motion.CCD2_Z.CurrentCmdPos - xyOffset, 1, Speed, 1, 1);
                    }
                }
            }        
        }

        private void CCD_Ctr(int ccdIdx, int axisID,int chanel)
        {
            if (CCD_Index == 1)
            {
                if (chanel == 1)
                {
                    if (axisID == 1)
                    {
                        ProcedureHelper_Exc.CCD1_X_ToCH1Mark = true;
                        ProcedureHelper_Exc.CCD1_Y_ToCH1Mark = true;
                        //motion.CCD1_X.AxisMoveAbs(ParameterHelper.CurParam.CCD1.CH1_Mark.CCD_X, 1, Speed, 1, 1);
                        //motion.CCD1_Y.AxisMoveAbs(ParameterHelper.CurParam.CCD1.CH1_Mark.CCD_Y, 1, Speed, 1, 1);
                    }
                    else if (axisID == 2)
                    {
                        ProcedureHelper_Exc.CCD1_X_ToCH1Work = true;
                        ProcedureHelper_Exc.CCD1_Y_ToCH1Work = true;

                        //motion.CCD1_X.AxisMoveAbs(ParameterHelper.CurParam.CCD1.CH1_Insp.CCD_X, 1, Speed, 1, 1);
                        //motion.CCD1_Y.AxisMoveAbs(ParameterHelper.CurParam.CCD1.CH1_Insp.CCD_Y, 1, Speed, 1, 1);
                    }
                }              
                else if (chanel == 2)
                {
                    if (axisID == 1)
                    {
                        ProcedureHelper_Exc.CCD1_X_ToCH2Mark = true;
                        ProcedureHelper_Exc.CCD1_Y_ToCH2Mark = true;
                        //motion.CCD1_X.AxisMoveAbs(ParameterHelper.CurParam.CCD1.CH2_Mark.CCD_X, 1, Speed, 1, 1);
                        //motion.CCD1_Y.AxisMoveAbs(ParameterHelper.CurParam.CCD1.CH2_Mark.CCD_Y, 1, Speed, 1, 1);
                    }
                    else if (axisID == 2)
                    {
                        ProcedureHelper_Exc.CCD1_X_ToCH2Work = true;
                        ProcedureHelper_Exc.CCD1_Y_ToCH2Work = true;
                        //motion.CCD1_X.AxisMoveAbs(ParameterHelper.CurParam.CCD1.CH2_Insp.CCD_X, 1, Speed, 1, 1);
                        //motion.CCD1_Y.AxisMoveAbs(ParameterHelper.CurParam.CCD1.CH2_Insp.CCD_Y, 1, Speed, 1, 1);
                    }
                }
                
            }
            else if (CCD_Index == 2)
            {
                if (chanel == 1)
                {
                    if (axisID == 1)
                    {
                        ProcedureHelper_Exc.CCD2_X_ToCH1Mark = true;
                        ProcedureHelper_Exc.CCD2_Y_ToCH1Mark = true;
                    }
                    else if (axisID == 2)
                    {
                        ProcedureHelper_Exc.CCD2_X_ToCH1Work = true;
                        ProcedureHelper_Exc.CCD2_Y_ToCH1Work = true;
                    }

                }
                else if (chanel == 2)
                {
                    if (axisID == 1)
                    {
                        ProcedureHelper_Exc.CCD2_X_ToCH2Mark = true;
                        ProcedureHelper_Exc.CCD2_Y_ToCH2Mark = true;
                    }
                    else if (axisID == 2)
                    {
                        ProcedureHelper_Exc.CCD2_X_ToCH2Work = true;
                        ProcedureHelper_Exc.CCD2_Y_ToCH2Work = true;
                    }                       
                }

            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (CCD_Index == 1)
            {
                lbl_ccdXAct.Text = "X:" + motion.CCD1_X.CurrentActPos.ToString("F3");
                lbl_ccdYAct.Text = "Y:" + motion.CCD1_Y.CurrentActPos.ToString("F3");
                lbl_ccdZAct.Text = "Z:" + motion.CCD1_Z.CurrentActPos.ToString("F3");
            }
            else if (CCD_Index == 2) 
            {
                lbl_ccdXAct.Text = "X:" + motion.CCD2_X.CurrentActPos.ToString("F3");
                lbl_ccdYAct.Text = "Y:" + motion.CCD2_Y.CurrentActPos.ToString("F3");
                lbl_ccdZAct.Text = "Z:" + motion.CCD2_Z.CurrentActPos.ToString("F3");
            }        
        }
    }
}
